package com.card.server;

import java.util.List;

import javax.jdo.PersistenceManager;

import com.card.client.entry.CreditCardBill;
import com.card.client.service.CreditCardBillService;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

@SuppressWarnings("unchecked")
public class CreditCardBillImpl extends RemoteServiceServlet implements	CreditCardBillService {

	private static final long serialVersionUID = 3680077381490457932L;

	@Override
	public void addCreditCardBill(CreditCardBill cardBill) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		if(cardBill!=null){
			pm.makePersistent(cardBill);
		}
		pm.close();
	}

	@Override
	public void delCreditCardBill(Long id) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		if(id!=null){
			String query = "select from " + CreditCardBill.class.getName() + " where id == " + id;
			List<CreditCardBill> result = (List<CreditCardBill>)pm.newQuery(query).execute(); 
			CreditCardBill cc = result.get(0);
			pm.deletePersistent(cc);
		}
		pm.close();
	}

	@Override
	public CreditCardBill[] getAllCreditCardBill() {
		CreditCardBill[] result = null;
		PersistenceManager pm = PMF.get().getPersistenceManager();
		List<CreditCardBill> list = (List<CreditCardBill>)pm.newQuery(CreditCardBill.class).execute();
		if(list!=null){
			result = new CreditCardBill[list.size()];
			for(int i=0;i<result.length;i++){
				result[i] = list.get(i);
			}
		} else {
			result = new CreditCardBill[0];
		}
		pm.close();
		return result;
	}

	@Override
	public void updateCreditCardBill(CreditCardBill cardBill) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		if(cardBill!=null){
			pm.makePersistent(cardBill);
		}
		pm.close();
	}

	@Override
	public CreditCardBill[] getFinanceMonthCreditCardBill(String financeMonth) {
		CreditCardBill[] result = null;
		PersistenceManager pm = PMF.get().getPersistenceManager();
		String query = "select from " + CreditCardBill.class.getName() + " where financeMonth == '" + financeMonth + "'";
		List<CreditCardBill> list = (List<CreditCardBill>)pm.newQuery(query).execute();
		if(list!=null){
			result = new CreditCardBill[list.size()];
			for(int i=0;i<result.length;i++){
				result[i] = list.get(i);
			}
		} else {
			result = new CreditCardBill[0];
		}
		pm.close();
		return result;
	}

}
